package donnees.dao;

import donnees.modele.Salle;
import donnees.outils.HibernateUtil;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

public class SalleDao implements SalleInterface {

    private Session session;

    @Override
    public List listerSalle() throws Exception {
        session = HibernateUtil.getSessionFactory().openSession();
        List results;
        Criteria cr = session.createCriteria(Salle.class);
        results = (List) cr.list();
        session.close();
        if (results.isEmpty()) {
            throw new Exception("Impossible de lister les salles !");
        } else {
            return results;
        }
    }

    @Override
    public Salle trouverSalle(String nom) throws Exception {
        session = HibernateUtil.getSessionFactory().openSession();
        Salle s;
        Criteria cr = session.createCriteria(Salle.class).add(Restrictions.eq("nom", nom));
        s = (Salle) cr.uniqueResult();
        session.close();
        if (s == null) {
            throw new Exception("Aucune salle ne porte ce nom.");
        } else {
            return s;
        }
    }
}
